package com.ruoyi.car.controller.admin.CarCustomer;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.CarCustomer.vo.CarCustomerAddReq;
import com.ruoyi.car.controller.admin.CarCustomer.vo.CarCustomerEditReq;
import com.ruoyi.car.controller.admin.CarCustomer.vo.CarCustomerReq;
import com.ruoyi.car.controller.admin.CarCustomer.vo.CarCustomerResp;
import com.ruoyi.car.controller.admin.company.vo.CarCompanyAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.entity.CarCompany;
import com.ruoyi.car.entity.CarCustomer;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.mapstruct.CarCompanyConvert;
import com.ruoyi.car.mapstruct.CarCustomerConvert;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.service.CarCustomerService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author wy
 * @Description 客户信息表
 * @Date 2023/6/6 16:11
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/car/customer")
@Api(tags = "管理后台-客户信息表")
public class CarCustomerController {

    @Autowired
    private CarCustomerService carCustomerService;

    @PreAuthorize(value = "@ss.hasPermi('info:customer:page')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/page")
    public TableDataInfo search(CarCustomerReq req){
        PageInfo<CarCustomerResp> pageInfo=this.carCustomerService.search(req);
        //总记录数
        long count= pageInfo.getTotal();
        TableDataInfo tableDataInfo=new TableDataInfo(pageInfo.getList(),count);
        return tableDataInfo;
    }


    @PreAuthorize(value = "@ss.hasPermi('info:customer:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarCustomerResp> findById(@PathVariable(value = "id") Long id){
        CarCustomerResp db = this.carCustomerService.findById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }
        return R.ok(db);
    }

    @Log(title = "客户信息表",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('info:customer:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CarCustomerAddReq req){
        //验证手机号码的唯一性
        int count=this.carCustomerService.checkPhoneUnique(null,req.getPhone());
        if(count>0){
            String msg=MessageUtils.message("customer.phone.exists");
            return R.fail(StringUtils.format(msg,req.getPhone()));
        }

        CarCustomer convert =  CarCustomerConvert.INSTANCE.convert(req);
        boolean save = this.carCustomerService.save(convert);
        return save? R.ok(): R.fail();
    }

    @Log(title = "客户信息表",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('info:customer:edit')")
    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Valid CarCustomerEditReq req){
        CarCustomer db = this.carCustomerService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        //验证手机号码的唯一性
        int count=this.carCustomerService.checkPhoneUnique(req.getId(),req.getPhone());
        if(count>0){
            String msg=MessageUtils.message("customer.phone.exists");
            return R.fail(StringUtils.format(msg,req.getPhone()));
        }

        CarCustomer convert =  CarCustomerConvert.INSTANCE.convert(req);

        boolean save = this.carCustomerService.updateById(convert);
        return save? R.ok(): R.fail();
    }

}
